package cn.edu.gduf.luowowo.website.web.controller;

import cn.edu.gduf.luowowo.comment.domain.ItemComment;
import cn.edu.gduf.luowowo.comment.service.IItemCommentService;
import cn.edu.gduf.luowowo.common.query.QueryObject;
import cn.edu.gduf.luowowo.common.util.AjaxResult;
import cn.edu.gduf.luowowo.member.domain.Item;
import cn.edu.gduf.luowowo.member.domain.ItemOrder;
import cn.edu.gduf.luowowo.member.domain.UserInfo;
import cn.edu.gduf.luowowo.member.service.IItermOrderService;
import cn.edu.gduf.luowowo.member.service.IItermService;
import cn.edu.gduf.luowowo.member.service.IUserInfoService;
import cn.edu.gduf.luowowo.website.annotation.UserParam;
import cn.edu.gduf.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Controller
@RequestMapping("/itemshop")
public class ItemShopController {

    @Reference
    private IItermService itermService;
    @Reference
    private IItermOrderService itermOrderService;
    @Reference
    private IUserInfoService userInfoService;
    @Reference
    private IItemCommentService itemCommentService;

    @RequestMapping("list")
    public Object list(Model model, QueryObject qo) {
        PageInfo pageInfo = itermService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        List<ItemComment> commentList=itemCommentService.findAll();
        model.addAttribute("commentList", commentList);
        return "itemshop/list";
    }

    @RequestMapping("/buy")
    @ResponseBody
    public Object buy(Long id, @UserParam UserInfo userInfo, HttpServletRequest request, HttpServletResponse response) {
        //用户未登录
        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "请登录后操作");
            return result;
        }
        //商品数量减1
        itermService.updateStorenum(-1, id);
        //创建订单，更新用户积分
        itermOrderService.insertOrder(id, userInfo.getId());
        //更新cookie
        UserInfo u = userInfoService.selectByPrimaryKey(userInfo.getId());
        request.getSession().setAttribute("userInfo", u);
        //更新redis用户信息，并延长时间
        String token = userInfoService.updateUserLogin(u);
        CookieUtil.addCookie(response, token);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/myitem")
    public Object myitem(Model model, @UserParam UserInfo userInfo) {
        if (userInfo != null) {
            ArrayList<Item> arrayList = new ArrayList<>();
            List<ItemOrder> list = itermOrderService.selectByUserId(userInfo.getId());
            HashMap<Long, Integer> map = new HashMap<>();
            if (list != null && list.size() > 0) {
                for (ItemOrder order : list) {
                    if (map.get(order.getItem().getId()) == null) {
                        map.put(order.getItem().getId(), 1);
                    } else {
                        map.put(order.getItem().getId(), map.get(order.getItem().getId()) + 1);
                    }
                }
            }
            Set<Map.Entry<Long, Integer>> entries = map.entrySet();
            for (Map.Entry<Long, Integer> entry : entries) {
                Item item = itermService.selectByPrimaryKey(entry.getKey());
                item.setNum(entry.getValue());
                arrayList.add(item);
            }
            model.addAttribute("list", arrayList);
        }

        return "itemshop/myItemTempl";
    }

    @RequestMapping("/comment")
    public Object comment(Model model,ItemComment itemComment, @UserParam UserInfo userInfo){
        if (userInfo != null) {
            itemComment.setUserId(userInfo.getId());
            itemComment.setUsername(userInfo.getNickname());
            itemComment.setCity(userInfo.getCity());
            itemComment.setLevel(userInfo.getLevel());
            itemComment.setHeadUrl(userInfo.getHeadImgUrl());
        }
        String id = itemCommentService.saveOrUpdate(itemComment);
        List<ItemComment> commentList=itemCommentService.findAll();
        model.addAttribute("commentList", commentList);
        return "itemshop/commentTempl";
    }

}
